﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Small.Util.Enum;

namespace Small.Util.Reflection
{
    public class Query
    {

        /// <summary>
        /// 反射获取到基本类型的数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="request">HttpRequest对象</param>
        /// <param name="elementID">生成HTML是前缀ID名称</param>
        /// <param name="method">标记为GET,还是Post方式提交</param>
        /// <returns></returns>
        public static T Result<T>(HttpRequest request,string elementName, QueryMethod method = QueryMethod.Post) where T : new()
        {

            T t =new T();
            var properties = t.GetType().GetProperties();
            foreach (var item in properties)
            {
                object temp = null;
                if (method == QueryMethod.Get)
                {
                    temp = request.QueryString[elementName + item.Name];


                }
                else
                {
                    temp = request.Form[elementName + item.Name];


                } if (temp != null)
                {

                    try
                    {
                        Type type = t.GetType().GetProperty(item.Name).PropertyType;


                        if (type == typeof(String))
                        {
                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToString(temp), null);
                        }
                        else if (type == typeof(Double) || type == typeof(double))
                        {
                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToDouble(temp), null);

                        }
                        else if (type == typeof(Int32) || type == typeof(int) || type == typeof(uint)
                                || type == typeof(UInt16) || type == typeof(UInt64)
                                || type == typeof(UInt32)
                                || type == typeof(Int16)
                                || type == typeof(UInt64)
                               )
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToInt32(temp), null);
                        }
                        else if (type == typeof(float))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToSingle(temp), null);
                        }
                        else if (type == typeof(Guid))
                        {

                           
                            t.GetType().GetProperty(item.Name).SetValue(t,  Guid.Parse(Convert.ToString(temp)), null);
                        }
                        else if (type == typeof(Byte) || type == typeof(byte))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToByte(temp), null);
                        }
                        else if (type == typeof(Boolean) || type == typeof(bool))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToBoolean(temp), null);
                        }
                        else if (type == typeof(char) || type == typeof(Char))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToChar(temp), null);
                        }
                        else if (type == typeof(DateTime))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToDateTime(temp), null);
                        }
                        else if (type == typeof(Decimal))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToDecimal(temp), null);
                        }
                        else if (type == typeof(long))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToInt64(temp), null);
                        }
                        else
                        {

                          
                        }


                    }
                    catch (Exception ex)
                    {

                        throw new Exception(ex.Message);
                    }
                }
            }

            return t;
        }

  



     /// <summary>
        /// 反射获取到基本类型的数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="request">HttpRequest对象</param>
        /// <param name="elementID">生成HTML是前缀ID名称</param>
        /// <param name="method">标记为GET,还是Post方式提交</param>
        /// <returns></returns>
        public static T Result<T>(HttpRequestBase request,string elementName, QueryMethod method = QueryMethod.Post)
            where T:new()  
        {

            T t = new T();
            var properties = t.GetType().GetProperties();
            foreach (var item in properties)
            {
                object temp = null;
                if (method == QueryMethod.Get)
                {
                    temp = request.QueryString[elementName + item.Name];


                }
                else
                {
                    temp = request.Form[elementName + item.Name];


                } if (temp != null)
                {

                    try
                    {
                        Type type = t.GetType().GetProperty(item.Name).PropertyType;


                        if (type == typeof(String))
                        {
                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToString(temp), null);
                        }
                        else if (type == typeof(Double) || type == typeof(double))
                        {
                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToDouble(temp), null);

                        }
                        else if (type == typeof(Int32) || type == typeof(int) || type == typeof(uint)
                                || type == typeof(UInt16) || type == typeof(UInt64)
                                || type == typeof(UInt32)
                                || type == typeof(Int16)
                                || type == typeof(UInt64)
                               )
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToInt32(temp), null);
                        }
                        else if (type == typeof(float))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToSingle(temp), null);
                        }
                        else if (type == typeof(Guid))
                        {

                           
                            t.GetType().GetProperty(item.Name).SetValue(t,  Guid.Parse(Convert.ToString(temp)), null);
                        }
                        else if (type == typeof(Byte) || type == typeof(byte))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToByte(temp), null);
                        }
                        else if (type == typeof(Boolean) || type == typeof(bool))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToBoolean(temp), null);
                        }
                        else if (type == typeof(char) || type == typeof(Char))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToChar(temp), null);
                        }
                        else if (type == typeof(DateTime))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToDateTime(temp), null);
                        }
                        else if (type == typeof(Decimal))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToDecimal(temp), null);
                        }
                        else if (type == typeof(long))
                        {

                            t.GetType().GetProperty(item.Name).SetValue(t, Convert.ToInt64(temp), null);
                        }
                        else
                        {

                          
                        }


                    }
                    catch (Exception ex)
                    {

                        throw new Exception(ex.Message);
                    }
                }
            }

            return t;
        }

    }
      }

